import tkinter as tk
from tkinter import ttk
import customtkinter
from ui.pages.base_page import BasePage
from core.utils.i18n import t
from ui.styles.theme import Theme

class WelcomePage(BasePage):
    def create_widgets(self):
        # 获取当前主题颜色
        bg_color = Theme.get_color('bg_tertiary')
        fg_primary = Theme.get_color('fg_primary')
        fg_secondary = Theme.get_color('fg_secondary')
        fg_tertiary = Theme.get_color('fg_tertiary')
        accent_color = Theme.get_color('accent')
        blue_color = Theme.get_color('accent') if Theme.is_dark_mode() else '#3498DB'
        
        # 主容器 - 居中显示
        main_container = customtkinter.CTkFrame(self, fg_color="transparent")
        main_container.place(relx=0.5, rely=0.5, anchor="center")
        
        # Logo和标题
        self.logo_label = customtkinter.CTkLabel(
            main_container,
            text=t("welcome.title"),
            font=("Arial", 32, "bold"),
            text_color=accent_color,
            bg_color="transparent"
        )
        self.logo_label.pack(pady=20)
        
        self.subtitle_label = customtkinter.CTkLabel(
            main_container,
            text=t("welcome.subtitle"), 
            font=("Arial", 14),
            text_color=fg_tertiary,
            bg_color="transparent"
        )
        self.subtitle_label.pack(pady=5)
        
        # 特色标签
        features_frame = customtkinter.CTkFrame(main_container, fg_color="transparent")
        features_frame.pack(pady=30)
        
        features = [
            t("welcome.features.shipping"), 
            t("welcome.features.prices"), 
            t("welcome.features.quality"), 
            t("welcome.features.security")
        ]
        
        self.feature_labels = []
        for feature in features:
            feature_label = customtkinter.CTkLabel(
                features_frame,
                text=feature,
                font=("Arial", 10, "bold"),
                text_color=blue_color,
                bg_color="transparent"
            )
            feature_label.pack(side="left", padx=15, pady=5)
            self.feature_labels.append(feature_label)
        
        # 开始购物按钮
        self.start_button = customtkinter.CTkButton(
            main_container,
            text=t("welcome.start_shopping"),
            command=self.start_shopping,
            width=200,
            height=40,
            font=("Arial", 14, "bold"),
            fg_color=accent_color,
            hover_color="#E55A2B",
            corner_radius=10
        )
        self.start_button.pack(pady=40)
        
        # 管理员按钮
        self.admin_button = customtkinter.CTkButton(
            main_container,
            text=t("welcome.admin_login"),
            command=self.go_to_admin_login,
            width=150,
            height=35,
            font=("Arial", 12),
            fg_color=("gray70", "gray30"),
            hover_color=("gray60", "gray40"),
            corner_radius=10
        )
        self.admin_button.pack(pady=5)
        
        # 底部提示
        self.hint_label = customtkinter.CTkLabel(
            main_container,
            text=t("welcome.hint"),
            font=("Arial", 9),
            text_color=fg_tertiary,
            bg_color="transparent"
        )
        self.hint_label.pack(pady=10)
    
    def start_shopping(self):
        self.controller.show_page("ProductPage")
    
    def go_to_admin_login(self):
        """跳转到管理员登录页面"""
        self.controller.show_page("AdminLoginPage")
    
    def refresh_language(self):
        """刷新语言显示"""
        try:
            # 更新文本内容
            self.logo_label.configure(text=t("welcome.title"))
            self.subtitle_label.configure(text=t("welcome.subtitle"))
            self.start_button.configure(text=t("welcome.start_shopping"))
            self.admin_button.configure(text=t("welcome.admin_login"))
            self.hint_label.configure(text=t("welcome.hint"))
            
            # 更新特色标签
            features = [
                t("welcome.features.shipping"), 
                t("welcome.features.prices"), 
                t("welcome.features.quality"), 
                t("welcome.features.security")
            ]
            
            for i, feature in enumerate(features):
                if i < len(self.feature_labels):
                    self.feature_labels[i].configure(text=feature)
                    
        except Exception as e:
            from core.utils.logger import error
            error(f"Failed to refresh language in WelcomePage: {e}")
